package com.zhm.demo.netty.init;


import com.zhm.demo.netty.handler.HeartbeatHandler;
import com.zhm.demo.netty.handler.ProtoServerHandler;
import com.zhm.demo.netty.protobuf.DeviceBean;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.protobuf.ProtobufDecoder;
import io.netty.handler.codec.protobuf.ProtobufEncoder;
import io.netty.handler.codec.protobuf.ProtobufVarint32FrameDecoder;
import io.netty.handler.codec.protobuf.ProtobufVarint32LengthFieldPrepender;

/**
 * 类说明：
 */
public class ServerInit extends ChannelInitializer<SocketChannel> {

    @Override
    protected void initChannel(SocketChannel ch) throws Exception {

        /*粘包半包*/
        /*粘包半包*/
//        ch.pipeline().addLast(new LengthFieldBasedFrameDecoder(65535,
//                0,2,0,
//                2));
//        ch.pipeline().addLast(new LengthFieldPrepender(2));

        //解码器，通过Google Protocol Buffers序列化框架动态的切割接收到的ByteBuf
        ch.pipeline().addLast(new ProtobufVarint32FrameDecoder());
        //服务器端接收的是客户端RequestUser对象，所以这边将接收对象进行解码生产实列
        ch.pipeline().addLast(new ProtobufDecoder(DeviceBean.WrapperMessage.getDefaultInstance()));
        //Google Protocol Buffers编码器
        ch.pipeline().addLast(new ProtobufVarint32LengthFieldPrepender());
        //Google Protocol Buffers编码器
        ch.pipeline().addLast(new ProtobufEncoder());
        //心跳处理
        ch.pipeline().addLast(new HeartbeatHandler());
        //业务处理。
        ch.pipeline().addLast(new ProtoServerHandler());
    }
}
